package main;

import java.io.File;

import org.softmed.action.Action;
import org.softmed.action.builder.ActionCreator;
import org.softmed.action.converter.DefaultXStreamActionConverter;
import org.softmed.action.description.ActionDescription;
import org.softmed.action.description.ActionStepDescription;
import org.softmed.action.guice.ActionModule;
import org.softmed.action.tests.PrintAction;
import org.softmed.action.tests.RecoverablePoint;
import org.softmed.action.tests.StopExecutionFlow;
import org.softmed.action.tests.ThrowTestException;
import org.softmed.action.tree.ActionContainerNode;
import org.softmed.action.tree.NodeNavigator;
import org.softmed.action.tree.ParentNode;
import org.softmed.filehandling.FileUtil;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.thoughtworks.xstream.XStream;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Injector injector = Guice.createInjector(new ActionModule());

		ActionCreator creator = injector.getInstance(ActionCreator.class);
		// app.execute();
		/*
		 * Action wf = new Action();
		 * 
		 * PrintAction print = new PrintAction("test action"); PrintAction
		 * printRecover = new PrintAction("recover action"); ThrowTestException
		 * excp = new ThrowTestException(); StopExecutionFlow stop = new
		 * StopExecutionFlow(); RecoverablePoint safe = new RecoverablePoint();
		 * 
		 * wf.getExecuting().addAction(print);
		 * wf.getExecuting().addAction(safe); wf.getExecuting().addAction(excp);
		 * wf.getExecuting().addAction(stop); wf.getExecuting().addAction(new
		 * PrintAction("resuming execution"));
		 * 
		 * wf.getRecovering().addAction(printRecover);
		 * 
		 * System.out.println(wf.isPossible());
		 * 
		 * ActionDescription ad = new ActionDescription(); ActionStepDescription
		 * asd = new ActionStepDescription();
		 * 
		 * asd = new ActionStepDescription();
		 * asd.setSource(PrintAction.class.getCanonicalName());
		 * asd.getParameters().add("test action");
		 * ad.getExecution().addActionStepDescriptor(asd);
		 * 
		 * asd = new ActionStepDescription();
		 * asd.setSource(RecoverablePoint.class.getCanonicalName());
		 * ad.getExecution().addActionStepDescriptor(asd);
		 * 
		 * asd = new ActionStepDescription();
		 * asd.setSource("org.softmed.action.tests.TestGroovyClass");
		 * asd.getParameters().add("parameters to groovy"); //
		 * asd.getParameters().add(true); // asd.getParameters().add(1);
		 * 
		 * ad.getExecution().addActionStepDescriptor(asd);
		 * 
		 * asd = new ActionStepDescription();
		 * asd.setSource(StopExecutionFlow.class.getCanonicalName());
		 * ad.getExecution().addActionStepDescriptor(asd);
		 * 
		 * asd = new ActionStepDescription();
		 * asd.setSource(ThrowTestException.class.getCanonicalName());
		 * ad.getExecution().addActionStepDescriptor(asd); //
		 */
		// try {
		// // wf.run();
		//
		// } catch (Throwable e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		System.out.println("stop");

		DefaultXStreamActionConverter converter = new DefaultXStreamActionConverter();

		// ActionDescription ad = new ActionDescription();
		// ActionStepDescription desc = new
		// ActionStepDescription("org.test.wtf.damn");
		// desc.setType(ActionStepDescription.TYPE.GROOVY);
		// ad.getTest().addActionStepDescriptor(desc);
		/*
		 * String xml = converter.convertToXML(ad); System.out.println(xml);
		 * 
		 * try {
		 * 
		 * Action action = creator.getActionFromXML(xml); action.clone().run();
		 * 
		 * // try { // wf.cancel(); // } catch (Throwable e) { // // TODO
		 * Auto-generated catch block // e.printStackTrace(); // } //
		 */
		/*
		 * ParentNode node = new ParentNode(); ActionContainerNode anode = new
		 * ActionContainerNode(); node.addAction("test",
		 * creator.getActionFromXML(xml));
		 * 
		 * NodeNavigator nav = new NodeNavigator(); Action t =
		 * nav.getAction("test", node); t.run(); } catch (Throwable e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } //
		 */
		try {
			/*
			 * ad = new ActionDescription(); asd = new ActionStepDescription();
			 * 
			 * asd = new ActionStepDescription();
			 * asd.setSource("org.softmed.action.tests.tester");
			 * ad.getTest().addActionStepDescriptor(asd);
			 * 
			 * asd = new ActionStepDescription();
			 * asd.setSource("org.softmed.action.tests.outputer");
			 * asd.getParameters().add(new Boolean(false));
			 * asd.getParameters().add(13);
			 * ad.getExecution().addActionStepDescriptor(asd);
			 * 
			 * asd = new ActionStepDescription();
			 * asd.setSource("org.softmed.action.tests.SimpleTester");
			 * ad.getTest().addActionStepDescriptor(asd);
			 * 
			 * xml = converter.convertToXML(ad);
			 * System.out.println("Groovy Script based system");
			 * System.out.println(xml);
			 * 
			 * Action action = creator.getActionFromXML(xml);
			 * action.clone().run(); //
			 */

			ActionDescription ad = new ActionDescription();
			ActionStepDescription asd = new ActionStepDescription();

			asd = new ActionStepDescription();
			asd.setSource("test1");
			ad.getTest().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("test2");
			ad.getTest().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("exe1");
			ad.getExecute().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("exe2");
			ad.getExecute().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("dis1");
			ad.getDispose().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("dis2");
			ad.getDispose().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("ef");
			ad.getExecutionFailure().addActionStepDescriptor(asd);

			asd = new ActionStepDescription();
			asd.setSource("ef2");
			ad.getExecutionFailure().addActionStepDescriptor(asd);

			String export = converter.convertToXML(ad);
			System.out.println(export);

			FileUtil util = new FileUtil();
			String xml = util.readFromFile("/src/GetPagedListXML.xml");

			Action action = creator.getActionFromXML(export);
			action.clone().run();

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
